package o;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.Toast;
import com.huawei.android.app.PackageManagerEx;
import com.huawei.appgallery.foundation.ui.framework.widget.dialog.dialogactivity.DialogActivity.a;
import com.huawei.fastengine.fastview.download.utils.PackageUtils;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.RETCODE;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import o.dky.j;

public class ceu {
    private static Map<String, Integer> ˋॱ = new HashMap();
    private String ʻ;
    private ces ʼ;
    private String ʽ;
    private String ˊ;
    private WeakReference<Activity> ˋ;
    private String ˎ;
    private int ˏ;
    private Handler ͺ = new Handler(this, Looper.getMainLooper()) {
        final /* synthetic */ ceu ˎ;

        public void handleMessage(Message message) {
            switch (message.what) {
                case 1000:
                    if (this.ˎ.ˏ(erz.ˎ(ContentManager.getInstant().getContext(), this.ˎ.ˎ))) {
                        this.ˎ.ॱ(2000);
                        return;
                    }
                    this.ˎ.ॱ((int) RETCODE.SIGN_IN_PARAMS_ERROR);
                    Activity activity = (Activity) this.ˎ.ˋ.get();
                    if (activity == null || activity.isFinishing()) {
                        Logger.ˎ("RestoreAppManager", "Restore success, but activity is invalid.");
                        this.ˎ.ॱ(2104);
                        return;
                    }
                    this.ˎ.ˊ();
                    ((cfj) cdb.ॱ(cfj.class)).ॱ(activity, this.ˎ.ˏ, this.ˎ.ˎ, this.ˎ.ˋ(3));
                    return;
                case 1001:
                    Toast.makeText(ContentManager.getInstant().getContext(), ContentManager.getInstant().getContext().getString(j.ʹ), 0).show();
                    this.ˎ.ॱ(2101);
                    return;
                default:
                    return;
            }
        }
    };
    private String ॱ;
    private b ॱॱ;
    private String ᐝ;

    class b extends SafeBroadcastReceiver {
        final /* synthetic */ ceu ˊ;

        private b(ceu o_ceu) {
            this.ˊ = o_ceu;
        }

        public void onReceiveMsg(Context context, Intent intent) {
            String action = intent.getAction();
            Logger.ˎ("RestoreAppManager", "receive broadcast: " + intent.getAction());
            if ("com.huawei.appmarket.thirdApp.install.successed".equals(action)) {
                this.ˊ.ॱ(2001);
            }
            if ("com.huawei.appmarket.thirdApp.install.failed".equals(action)) {
                this.ˊ.ॱ(2103);
            }
            if ("com.huawei.appmarket.thirdApp.download.failed".equals(action)) {
                this.ˊ.ॱ(2102);
            }
            if ("com.huawei.appmarket.thirdApp.paused.network.changed".equals(action) || "com.huawei.appmarket.thirdApp.no.network".equals(action)) {
                this.ˊ.ॱ(2102);
            }
            if ("com.huawei.appmarket.thirdApp.cannot.update".equals(action)) {
                Toast.makeText(ContentManager.getInstant().getContext(), ContentManager.getInstant().getContext().getString(j.ʿॱ), 0).show();
                this.ˊ.ॱ(2100);
            }
            if ("com.huawei.appmarket.thirdApp.show.inqure.dialog".equals(action)) {
                this.ˊ.ॱ(2403);
            }
            if ("com.huawei.appmarket.thirdApp.install.confirm".equals(action)) {
                this.ˊ.ॱ(2404);
            }
            if ("com.huawei.appmarket.thirdApp.install.cancel".equals(action) || "com.huawei.appmarket.thirdApp.install.interrupt".equals(action)) {
                this.ˊ.ॱ(2301);
            }
            if ("com.huawei.appmarket.thirdApp.activity.finished".equals(action)) {
                this.ˊ.ॱ(2405);
            }
        }
    }

    static {
        ˋॱ.put(PackageUtils.HWGAMEBOX_PACKAGE_NAME, Integer.valueOf(j.ꜟ));
        ˋॱ.put("com.huawei.educenter", Integer.valueOf(j.ꜞ));
    }

    public ceu(Activity activity) {
        this.ˋ = new WeakReference(activity);
    }

    public void ˊ(cet o_cet, ces o_ces) {
        this.ˎ = o_cet.ˎ();
        this.ˊ = o_cet.ˏ();
        this.ˏ = o_cet.ˋ();
        this.ʻ = o_cet.ˊ();
        this.ᐝ = o_cet.ॱ();
        this.ʽ = o_cet.ॱॱ();
        this.ʼ = o_ces;
        if (!ˏ()) {
            return;
        }
        if (erz.ˏ(ContentManager.getInstant().getContext(), this.ˎ)) {
            int ˎ = erz.ˎ(ContentManager.getInstant().getContext(), this.ˎ);
            if (ˏ(ˎ)) {
                Logger.ˎ("RestoreAppManager", "current version:" + ˎ + ", target version:" + this.ˏ);
                ॱ(2002);
                return;
            }
            Activity activity = (Activity) this.ˋ.get();
            if (activity == null || activity.isFinishing()) {
                Logger.e("RestoreAppManager", "Try to update, input param, activity is invalid.");
                ॱ(2103);
                return;
            }
            ˊ();
            ((cfj) cdb.ॱ(cfj.class)).ॱ(activity, this.ˏ, this.ˎ, ˋ(3));
        } else if (ˎ()) {
            ˋ();
        } else {
            Activity activity2 = (Activity) this.ˋ.get();
            if (activity2 == null || activity2.isFinishing()) {
                Logger.e("RestoreAppManager", "Try to download, input param, activity is invalid.");
                ॱ(2103);
                return;
            }
            ˊ();
            ((cfj) cdb.ॱ(cfj.class)).ˊ(activity2, this.ˏ, this.ˎ, ˋ(2), 0);
        }
    }

    private boolean ˏ() {
        if (this.ʼ == null) {
            Logger.e("RestoreAppManager", "null callback");
            return false;
        } else if (TextUtils.isEmpty(this.ˎ)) {
            Logger.e("RestoreAppManager", "null packageName");
            ॱ(2202);
            return false;
        } else if (TextUtils.isEmpty(this.ˊ)) {
            Logger.e("RestoreAppManager", "null preInstallPathJudge");
            ॱ(2201);
            return false;
        } else {
            this.ˊ = this.ˊ.toLowerCase(Locale.US);
            if (this.ˏ <= 0) {
                Logger.e("RestoreAppManager", "invalid target version=" + this.ˏ);
                ॱ(2200);
                return false;
            }
            Logger.ˎ("RestoreAppManager", "restore params: packageName=" + this.ˎ + ", preInstallPathJudge=" + this.ˊ + ", targetVersion=" + this.ˏ);
            return true;
        }
    }

    private boolean ˎ() {
        if (azc.ˎ().ॱ() < 22) {
            Logger.ˎ("RestoreAppManager", "Emui version=" + azc.ˎ().ॱ());
            return false;
        }
        List<String> scanInstallList = PackageManagerEx.getScanInstallList();
        if (scanInstallList == null) {
            Logger.e("RestoreAppManager", "preInstallList is null");
            return false;
        }
        for (String str : scanInstallList) {
            Logger.ˎ("RestoreAppManager", "preInstall path: " + str);
            if (str.toLowerCase(Locale.US).contains(this.ˊ) && ॱ(str)) {
                this.ॱ = str;
                return true;
            }
        }
        return false;
    }

    private boolean ॱ(String str) {
        Logger.ˎ("RestoreAppManager", "Check preInstall path: " + str);
        Object obj = "";
        File file = new File(str);
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            if (listFiles == null) {
                Logger.e("RestoreAppManager", "No file list.");
                return false;
            }
            for (File file2 : listFiles) {
                if (!file2.isDirectory()) {
                    obj = ˏ(file2);
                    if (!TextUtils.isEmpty(obj)) {
                        break;
                    }
                }
            }
        } else {
            obj = ˏ(file);
        }
        if (!TextUtils.isEmpty(obj)) {
            PackageInfo packageArchiveInfo = ContentManager.getInstant().getContext().getPackageManager().getPackageArchiveInfo(obj, 0);
            Logger.ˎ("RestoreAppManager", "pkgInfoName=" + packageArchiveInfo.packageName);
            if (packageArchiveInfo.packageName.equalsIgnoreCase(this.ˎ)) {
                return true;
            }
        }
        return false;
    }

    private String ˏ(File file) {
        String str = "";
        String name = file.getName();
        int lastIndexOf = name.lastIndexOf(".");
        if (-1 != lastIndexOf) {
            Object substring = name.substring(lastIndexOf + 1);
            if (!TextUtils.isEmpty(substring) && substring.equalsIgnoreCase("apk")) {
                try {
                    str = file.getCanonicalPath();
                } catch (IOException e) {
                    Logger.e("RestoreAppManager", "get find apk path IOException:" + e.getMessage());
                }
            }
        }
        return str;
    }

    private void ˋ() {
        Activity activity = (Activity) this.ˋ.get();
        if (activity == null || activity.isFinishing()) {
            Logger.e("RestoreAppManager", "Prepare to show restore dialog, but activity is invalid.");
            Toast.makeText(ContentManager.getInstant().getContext(), ContentManager.getInstant().getContext().getString(j.ʹ), 0).show();
            ॱ(2101);
            return;
        }
        new a(activity, "RestoreAppDialog").ॱ(ˋ(1)).ˋ(-1, j.ﾞ).ˋ(-2, j.ᶥ).ˏ(new ckh.a(this) {
            final /* synthetic */ ceu ˏ;

            {
                this.ˏ = r1;
            }

            public void ˏ(AlertDialog alertDialog, a aVar, int i) {
                if (-1 == i) {
                    if (alertDialog != null) {
                        alertDialog.dismiss();
                    }
                    this.ˏ.ॱ(2401);
                    dmq.ˏ.ˊ(dmp.CONCURRENT, new dmi(this) {
                        final /* synthetic */ AnonymousClass4 ˏ;

                        {
                            this.ˏ = r1;
                        }

                        public void run() {
                            boolean scanInstallApk = PackageManagerEx.scanInstallApk(this.ˏ.ˏ.ॱ);
                            this.ˏ.ˏ.ॱ = null;
                            Logger.ˎ("RestoreAppManager", "Restore result:" + scanInstallApk);
                            if (scanInstallApk) {
                                ((cdf) cdb.ॱ(cdf.class)).ˏ(this.ˏ.ˏ.ˎ);
                                this.ˏ.ˏ.ͺ.sendEmptyMessage(1000);
                                return;
                            }
                            this.ˏ.ˏ.ͺ.sendEmptyMessage(1001);
                        }
                    });
                } else if (-2 == i) {
                    if (alertDialog != null) {
                        alertDialog.dismiss();
                    }
                    this.ˏ.ॱ(2300);
                }
            }
        }).ˏ(new OnKeyListener(this) {
            final /* synthetic */ ceu ˎ;

            {
                this.ˎ = r1;
            }

            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (i != 4 || keyEvent.getAction() != 0) {
                    return false;
                }
                Logger.ˎ("RestoreAppManager", "click key back, installFail dialog dismiss.");
                dialogInterface.dismiss();
                this.ˎ.ॱ(2300);
                return true;
            }
        }).ˎ();
        ॱ(2400);
    }

    private boolean ˏ(int i) {
        return i >= this.ˏ;
    }

    private void ˊ() {
        if (this.ॱॱ == null) {
            Logger.ˎ("RestoreAppManager", "Register broadcast.");
            this.ॱॱ = new b();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("com.huawei.appmarket.thirdApp.install.successed");
            intentFilter.addAction("com.huawei.appmarket.thirdApp.install.failed");
            intentFilter.addAction("com.huawei.appmarket.thirdApp.download.failed");
            intentFilter.addAction("com.huawei.appmarket.thirdApp.install.confirm");
            intentFilter.addAction("com.huawei.appmarket.thirdApp.install.cancel");
            intentFilter.addAction("com.huawei.appmarket.thirdApp.install.interrupt");
            intentFilter.addAction("com.huawei.appmarket.thirdApp.paused.network.changed");
            intentFilter.addAction("com.huawei.appmarket.thirdApp.cannot.update");
            intentFilter.addAction("com.huawei.appmarket.thirdApp.show.inqure.dialog");
            intentFilter.addAction("com.huawei.appmarket.thirdApp.no.network");
            intentFilter.addAction("com.huawei.appmarket.thirdApp.activity.finished");
            ll.ˏ(ContentManager.getInstant().getContext()).ˏ(this.ॱॱ, intentFilter);
        }
    }

    private void ॱ() {
        if (this.ॱॱ != null) {
            Logger.ˎ("RestoreAppManager", "Unregister broadcast.");
            ll.ˏ(ContentManager.getInstant().getContext()).ˋ(this.ॱॱ);
            this.ॱॱ = null;
        }
    }

    private String ˋ(int i) {
        String ˏ = ˏ(this.ˎ);
        switch (i) {
            case 1:
                if (!TextUtils.isEmpty(this.ʻ)) {
                    return this.ʻ;
                }
                return ContentManager.getInstant().getContext().getString(j.ﾟ, new Object[]{ˏ});
            case 2:
                if (!TextUtils.isEmpty(this.ᐝ)) {
                    return this.ᐝ;
                }
                return ContentManager.getInstant().getContext().getString(j.ـ, new Object[]{ˏ});
            case 3:
                if (!TextUtils.isEmpty(this.ʽ)) {
                    return this.ʽ;
                }
                return ContentManager.getInstant().getContext().getString(j.ʾॱ, new Object[]{ˏ});
            default:
                return ˏ;
        }
    }

    private String ˏ(String str) {
        Integer num = (Integer) ˋॱ.get(str);
        if (num != null) {
            return ContentManager.getInstant().getContext().getString(num.intValue());
        }
        return "";
    }

    private void ॱ(int i) {
        switch (i) {
            case 2400:
            case 2401:
            case 2403:
            case 2404:
                break;
            default:
                ॱ();
                break;
        }
        Logger.ˎ("RestoreAppManager", "Restore resultCode=" + i);
        this.ʼ.onResult(i);
    }
}
